﻿using SqlPerfomance.V2.DAL;
using SqlPerfomance.V2.DomainModel.Enums;
using SqlPerfomance.V2.DomainModel.Models;
using SqlPerfomance.V2.DomainModel.NodeTree.Interfaces;
using SqlPerfomance.V2.WpfClient.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SqlPerfomance.V2.WpfClient
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class AdjancyList : Window
	{
		public AdjancyList(ConnectionStrings connectionString)
		{
			InitializeComponent();

			var alRepo = new AdjancyListTreeRepository();

			var tree = alRepo.LoadAlTreeFromSqlDatabase(connectionString, AppSettingsHelper.AlTableName).ToList();
			var treeView = CreateTree(tree);
			trvMenu.Items.Add(treeView);
		}

		private AlTreeViewNode CreateTree(List<AdjancyListTreeNode> tree)
		{
			var root = tree.Where(t => t.Parent_Id == 0).First();
			AlTreeViewNode treeView = new AlTreeViewNode(root.Id, (long)root.Parent_Id, root.Name);
			tree.Remove(root);
			GetTreeViewData(treeView, tree);
			return treeView;
		}

		private void GetTreeViewData(AlTreeViewNode treeView, List<AdjancyListTreeNode> tree)
		{
			var children = tree.Where(t => t.Parent_Id == treeView.Id).ToList();
			for (int i = 0; i < children.Count; i++)
			{
				var child = new AlTreeViewNode(children[i].Id, (long)children[i].Parent_Id, children[i].Name);
				treeView.Children.Add(child);
				GetTreeViewData(child, tree);
			}
		}
	}

	//public class AlTreeViewNode
	//{
	//	public AlTreeViewNode() { }

	//	public AlTreeViewNode(int id, long parent_id, string name)
	//	{
	//		Id = id;
	//		Parent_Id = parent_id;
	//		Name = name;
	//		this.Children = new ObservableCollection<AlTreeViewNode>();
	//	}

	//	public int Id { get; set; }
	//	public long Parent_Id { get; set; }
	//	public string Name { get; set; }

	//	public ObservableCollection<AlTreeViewNode> Children { get; set; }
	//}
}
